package com.lc.rongzecaifu.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.conn.JsonAccessToken;
import com.lc.rongzecaifu.conn.JsonBanklist;
import com.lc.rongzecaifu.conn.JsonWithdraw;
import com.lc.rongzecaifu.conn.JsonWithdrawvalidate;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.toast.LovelyToast;
import com.lc.rongzecaifu.ui.RegistWebActivity;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/25.
 */

public class NewWithDrawActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivback;
    private TextView tvtitleright;

    private TextView tv01;
    private TextView tv02;
    private ImageView iv01;
    private ImageView iv02;

    private TextView tv03;
    private EditText et01;
    private EditText et02;
    private LinearLayout ll01;
    private Button bt01;

    private boolean Mode = false;//默认提现模式


    private List<JsonBanklist.Info.ListContent> list = new ArrayList<>();

    private String msg;
    private PopupWindow popWindow;


    public String id;

    Double db_money;
    int db_money_int;

    private PopupWindow popWindow2;

    //false为充值
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newdraw);


        initView();
        initMode();
        initListener();

    }

    private void initView() {

        this.bt01 = (Button) findViewById(R.id.bt_01);
        this.ll01 = (LinearLayout) findViewById(R.id.ll_01);
        this.et02 = (EditText) findViewById(R.id.et_02);
        this.et01 = (EditText) findViewById(R.id.et_01);
        this.tv03 = (TextView) findViewById(R.id.tv_03);
        this.iv02 = (ImageView) findViewById(R.id.iv_02);
        this.iv01 = (ImageView) findViewById(R.id.iv_01);
        this.tv02 = (TextView) findViewById(R.id.tv_02);
        this.tv01 = (TextView) findViewById(R.id.tv_01);


        this.tvtitleright = (TextView) findViewById(R.id.tv_title_right);
        this.ivback = (ImageView) findViewById(R.id.iv_back);


        tv02.setText(MyApplication.myPreferences.readAccount_money() + "元");
    }

    private void initMode() {

    }

    private void initListener() {

        //lladd.setOnClickListener(this);
        tvtitleright.setOnClickListener(this);
        bt01.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ll_bank:

                intent.putExtra("mode", 0);
                intent.setClass(this, ChoseBankActivity.class);
                startActivityForResult(intent, 0);
                break;
            case R.id.ll_add:

                intent.setClass(this, BankcardAddActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_title_right:

                intent.putExtra("mode", true);//true为提现，false为充值

                intent.setClass(this, WithdrawRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_01:
                if (MyApplication.myPreferences.readStatus().equals("9")) {
                    showPopupWindow2(bt01);
                } else if (TextUtils.isEmpty(et01.getText().toString().trim())) {
                    //  Toast.makeText(context, "请输入提现金额", Toast.LENGTH_SHORT).show();
                    LovelyToast.makeText(NewWithDrawActivity.this, "请输入提现金额", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!TextUtils.isEmpty(et01.getText().toString().trim()) && Double.valueOf(et01.getText().toString().trim()).intValue() < 100) {

                    LovelyToast.makeText(NewWithDrawActivity.this, "提现金额最低为100", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!TextUtils.isEmpty(et01.getText().toString().trim()) && et01.getText().toString().trim().contains(".") && (et01.getText().toString().trim().length() - et01.getText().toString().trim().indexOf(".") - 1 > 2)) {
                    LovelyToast.makeText(NewWithDrawActivity.this, "您输入的充值数额不对，请重新输入", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(et02.getText().toString().trim())) {
                    LovelyToast.makeText(NewWithDrawActivity.this, "支付密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    getWithdrawvalidateAccessToken();
                }



/*

                else if (!TextUtils.isEmpty(tv01.getText().toString().trim()) && tv01.getText().toString().trim().contains(".") && Double.valueOf(tv01.getText().toString().trim()).intValue()<100){
                    LovelyToast.makeText(NewWithDrawActivity.this, "提现金额最低为100", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }else if (tv01.getText().toString().trim().contains(".") && (tv01.getText().toString().trim().length() - tv01.getText().toString().trim().indexOf(".") - 1)>2){
                    LovelyToast.makeText(NewWithDrawActivity.this, "您输入的充值数额不对，请重新输入", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }else if (TextUtils.isEmpty(et02.getText().toString().trim())){
                    LovelyToast.makeText(NewWithDrawActivity.this, "支付密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }

                else {
                    getWithdrawvalidateAccessToken();
                }
*/


/*

                else if (!TextUtils.isEmpty(tv01.getText().toString().trim())){
                    String pay_str2 = et01.getText().toString().trim();
                    Double d=Double.valueOf(pay_str2);
                    int a=d.intValue();
                    if (a<100){
                        LovelyToast.makeText(NewWithDrawActivity.this, "提现金额最低为100", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }else if (pay_str2.contains(".") && (pay_str2.length() - pay_str2.indexOf(".") - 1)>2){
                        LovelyToast.makeText(NewWithDrawActivity.this, "您输入的充值数额不对，请重新输入", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }else {
                        getWithdrawvalidateAccessToken();
                    }
                } else if (TextUtils.isEmpty(et02.getText().toString().trim())) {
                //    Toast.makeText(context, "请输入支付密码", Toast.LENGTH_SHORT).show();
                    LovelyToast.makeText(NewWithDrawActivity.this, "请输入支付密码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);

                } else if (TextUtils.isEmpty(tv02.getText().toString().trim())) {
                  //  Toast.makeText(context, "您的账户余额不足", Toast.LENGTH_SHORT).show();
                    LovelyToast.makeText(NewWithDrawActivity.this, "您的账户余额不足", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }

                else {
                    getWithdrawvalidateAccessToken();


                }

*/

                break;
        }

    }


    private void getWithdrawvalidateAccessToken() {
        new JsonAccessToken("user/withdraw_validate", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initWithdrawvalidate(info.access_token);
                    } else {
                        //UtilToast.show(NewWithDrawActivity.this, "安全验证失败！");
                        LovelyToast.makeText(NewWithDrawActivity.this, "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //  UtilToast.show(NewWithDrawActivity.this, "安全验证失败！");
                LovelyToast.makeText(NewWithDrawActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this, true);

    }

    String status;

    private void initWithdrawvalidate(String access_token) {
        new JsonWithdrawvalidate(access_token, MyApplication.myPreferences.readUid(), et01.getText().toString(), et02.getText().toString(), new AsyCallBack<JsonWithdrawvalidate.Info>() {

            @Override
            public void onSuccess(String toast, int type, JsonWithdrawvalidate.Info info) throws Exception {
                status = info.status;
                if (info != null) {


                    msg = info.tips;
                    bt01.setSelected(true);

                    LovelyToast.makeText(NewWithDrawActivity.this, msg, 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                if (JsonWithdrawvalidate.TOAST.equals("支付密码错误")) {
                    startActivity(new Intent(NewWithDrawActivity.this, PasswordTransactionActivity.class));
                }else {
                    bt01.setSelected(false);
                    msg = JsonWithdrawvalidate.TOAST;
              /*  if (!TextUtils.isEmpty(msg)) {
                    showPopupWindow1(bt01);
                }*/
                    LovelyToast.makeText(NewWithDrawActivity.this, msg, 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }



            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }).execute(this, true);


    }


    private void showPopupWindow1(View parent) {

        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                .inflate(R.layout.dialog_02, null)));
        //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
        popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        initPop1(view);

        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);

    }

    public void initPop1(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册
        final TextView tvmsg = (TextView) view.findViewById(R.id.tv_msg);
        tvmsg.setText(msg);
//        final LinearLayout linearlayout_dialog2 = (LinearLayout) findViewById(R.id.linearlayout_dialog2);
        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                popWindow.dismiss();


            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (bt01.isSelected()) {
                    getWithdrawAccessToken();
                }
                popWindow.dismiss();

            }
        });
    }

    private void getWithdrawAccessToken() {
        new JsonAccessToken("user/withdraw", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initWithdraw(info.access_token);
                    } else {
                        //  UtilToast.show(NewWithDrawActivity.this, "安全验证失败！");
                        LovelyToast.makeText(NewWithDrawActivity.this, "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //   UtilToast.show(NewWithDrawActivity.this, "安全验证失败！");
                LovelyToast.makeText(NewWithDrawActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this, true);

    }

    private void initWithdraw(String access_token) {
      /*  String str = et01.getText().toString().trim();
        Double d = Double.valueOf(str);
        DecimalFormat df = new DecimalFormat( "0.00 ");
        String str2 = df.format(d);*/
        new JsonWithdraw(access_token, MyApplication.myPreferences.readUid(), et01.getText().toString(), et02.getText().toString(), new AsyCallBack<JsonWithdraw.Info>() {

            @Override
            public void onSuccess(String toast, int type, JsonWithdraw.Info info) throws Exception {
                if (info != null) {
                    msg = info.tips;
                    tv02.setText(info.account_money + "元");
                    MyApplication.myPreferences.saveAccount_money(info.account_money);
                    bt01.setSelected(false);
                    if (!TextUtils.isEmpty(msg)) {
                        showPopupWindow1(bt01);
                    }

                    // Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                    LovelyToast.makeText(NewWithDrawActivity.this, msg, 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    finish();

                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                bt01.setSelected(false);
                msg = JsonWithdraw.TOAST;
                if (!TextUtils.isEmpty(msg)) {
                    showPopupWindow1(bt01);
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }).execute(this, true);

    }


    private void showPopupWindow2(View parent) {
        if (popWindow2 == null) {
            View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_kaihu, null)));
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow2 = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop2(view);
        }
        //设置动画效果
        popWindow2.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow2.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow2.setOutsideTouchable(true);
        popWindow2.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow2.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow2.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public void initPop2(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册


        final LinearLayout linear_dialog3 = (LinearLayout) view.findViewById(R.id.linear_dialog3);

        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                popWindow2.dismiss();
                popWindow2 = null;
                finish();
            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent();
                intent.putExtra("url", MyApplication.myPreferences.readRegistUrl());
                intent.setClass(NewWithDrawActivity.this, RegistWebActivity.class);
                startActivity(intent);
                popWindow2.dismiss();
            }
        });
    }


}
